#-*- coding: utf-8 -*- 

'''
Created on 2011. 1. 3.

@author: Wonderer
'''
import sys
import datetime
from Finance import Finance
from FinanceType import FinanceType

class FinanceManager:
    '''
    classdocs
    '''

    stock = []
    bond = []
    fund = []
    deposit = []
    financeList = []
    
    totalAppraisedMoney = 0
    totalBuyMoney = 0
    totalStockMoney = 0
    totalDepositMoney = 0
    totalBondMoney = 0
    totalFundMoney = 0

    def readFinanceFileData(self, fileName):

        f = open(fileName);
        for line in f.readlines():
            itemList = line.split(',')
            finance = Finance()
            financeType = int(itemList[0])
            finance.name = itemList[1] 
            finance.buyPrice = int(itemList[2])
            finance.appraisedPrice = int(itemList[3])
            finance.buyDate = datetime.datetime(2011,1,1)
            self.financeList.append(finance)
           
            #현금
            if financeType == FinanceType.Deposit:
                finance.type = FinanceType.Deposit
                self.deposit.append(finance)
                self.totalDepositMoney += finance.buyPrice
            #채권
            elif financeType == FinanceType.Bond:
                finance.type = FinanceType.Bond
                self.bond.append(finance)
                self.totalBondMoney += finance.buyPrice
            #펀드
            elif financeType == FinanceType.Fund:
                finance.type = FinanceType.Fund
                self.fund.append(finance)
                self.totalFundMoney += finance.buyPrice
            #주식
            elif financeType == FinanceType.Stock:
                finance.type = FinanceType.Stock
                self.stock.append(finance)
                self.totalStockMoney += finance.buyPrice
                
            self.totalAppraisedMoney += finance.appraisedPrice
            self.totalBuyMoney += finance.buyPrice

                
        f.close()
    
    def saveFile(self):
        f = open('result.csv', 'w+')
        stdout = sys.stdout
        sys.stdout = f
        
        financeType = {FinanceType.Deposit:'현금', FinanceType.Bond: '채권', FinanceType.Fund:'펀드', FinanceType.Stock:'주식'}
        print('분류,이름,원금,평가액,수익률,연수익률,원금비율')
        for finance in self.financeList:
            elapsedDay = (datetime.datetime.today() - finance.buyDate).days
            profitRateForYear = finance.getProfitRate() / (elapsedDay/365)
            retString = '%s,%s,%s,%s,%.2f%%,%.2f%%,%.2f%%' % (financeType.get(finance.type), finance.name,finance.buyPrice,finance.appraisedPrice, 
                                                              finance.getProfitRate(),profitRateForYear,
                                                               (finance.buyPrice/self.totalBuyMoney*100))
            print(retString)
        
        print('\n')
        print(',현금비중,%s,,,%.2f%%' % (self.totalDepositMoney, self.totalDepositMoney / self.totalBuyMoney*100) )
        print(',채권비중,%s,,,%.2f%%' % (self.totalBondMoney, self.totalBondMoney / self.totalBuyMoney*100) )
        print(',펀드비중,%s,,,%.2f%%' % (self.totalFundMoney, self.totalFundMoney / self.totalBuyMoney*100) )
        print(',주식비중,%s,,,%.2f%%' % (self.totalStockMoney, self.totalStockMoney / self.totalBuyMoney*100) )

        print('\n')
        
        margin = self.totalAppraisedMoney-self.totalBuyMoney
        print(',원금 ,%s' % self.totalBuyMoney)
        print(',평가액,,%s' % self.totalAppraisedMoney)
        print(',수익금,,,%s' % margin)
        print(',수익률 ,,,,%.2f%%' % (self.totalAppraisedMoney / self.totalBuyMoney*100-100))
        
        goalRate = 0.1
        
        print('\n')
        print(',,목표금액,남은금액,달성율')
        for year in range(1,21):
            goalMoney = ((self.totalBuyMoney + (year*30000000))* ((1+goalRate)**(year)))
            print(',%d년후,%d,%d,%.2f%%' %  (year,goalMoney, goalMoney-self.totalAppraisedMoney, self.totalAppraisedMoney/goalMoney*100))
        sys.stdout = stdout
        
        
        f.close()
